----------------------------------------------------------------------------------------------------------------------------------
--[[

    长柄精通

    带突刺路线 目标扫描、伤害执行函数

    带 突刺可用武器类型 控制器。


]]--
----------------------------------------------------------------------------------------------------------------------------------
--
    local function GetReplica(self)
        return self.inst.replica.hutao_com_polearm_mastery or self.inst.replica._.hutao_com_polearm_mastery
    end
    local function set_current(self, value)
        local replica = GetReplica(self)
        if replica then
            replica:SetCurrent(value)
            self:DataSync()
        end
    end
    local function set_max(self,value)
        local replica = GetReplica(self)
        if replica then
            replica:SetMax(value)
            self:DataSync()
        end
    end    
----------------------------------------------------------------------------------------------------------------------------------
local hutao_com_polearm_mastery = Class(function(self, inst)
    self.inst = inst

    self.DataTable = {}
    self.TempTable = {}
    self._onload_fns = {}
    self._onsave_fns = {}


    -------------------------------------------------------
    -- 铲子充能
        self.current = 0
        self.max = 12
        self:AddOnSaveFn(function(self)
            self:Set("current",self.current)
            self:Set("max",self.max)
        end)
        self:AddOnLoadFn(function(self)
            self.current = self:Get("current",0)/4*3
            self.max = self:Get("max",12)
        end)
    -------------------------------------------------------
    -- 突刺伤害倍增器
        self.spike_damage_multipliers = SourceModifierList(self.inst) -- damage dealt to others multiplier
    -------------------------------------------------------
    -- 武器记忆
        self.spike_weapons = {}
        self.shovel_weapons = {}
        self.spike_damage_fns = {}
        inst:ListenForEvent("equip",function(inst)
            inst:DoTaskInTime(0,function()
                self:DataSync()
            end)
        end)
    -------------------------------------------------------
    ---
        self.is_running = false
        self.spike_casting = false
    -------------------------------------------------------


end,
nil,
{
    current = set_current,
    max = set_max,
})
------------------------------------------------------------------------------------------------------------------------------
---
    local sync_fn = function(inst,self)
        local replica = GetReplica(self)
        if replica then
            local data = {
                current = self.current,
                max = self.max,
                spike_weapons = self.spike_weapons,
                shovel_weapons = self.shovel_weapons,
                is_running = self.is_running,
                test = math.random(100),
            }
            replica:SetData(data)
        end
    end
    function hutao_com_polearm_mastery:DataSync()
        self.inst:DoTaskInTime(0,sync_fn,self)
    end
------------------------------------------------------------------------------------------------------------------------------
--- 武器类型判定
    function hutao_com_polearm_mastery:AddSpikeWeapon(prefab)
        self.spike_weapons[prefab] = true
        self:DataSync()
    end
    function hutao_com_polearm_mastery:IsSpikeWeapon(prefab)
        return self.spike_weapons[prefab] == true
    end
    function hutao_com_polearm_mastery:AddShovelWeapon(prefab)
        self.shovel_weapons[prefab] = true
        self:DataSync()
    end
    function hutao_com_polearm_mastery:IsShovelWeapon(prefab)
        return self.shovel_weapons[prefab] == true
    end
    function hutao_com_polearm_mastery:AddSpikeDamageFn(prefab,fn)
        self.spike_damage_fns[prefab] = fn
    end
    function hutao_com_polearm_mastery:GetSpikeDamageFn(prefab)
        return self.spike_damage_fns[prefab]
    end
------------------------------------------------------------------------------------------------------------------------------
--- 突刺路线上的伤害函数。
    function hutao_com_polearm_mastery:SetSpikeCastingDamageFn(fn)      -- 群体伤害执行函数
        self.spike_casting_damage_fn = fn
    end
    function hutao_com_polearm_mastery:DoSpikeCastingDamage(targets)    -- 群体伤害执行函数
        if self.spike_casting_damage_fn then
            self.spike_casting_damage_fn(self.inst,targets)
        end
    end
    function hutao_com_polearm_mastery:SetSpikeLineTargetSearchFn(fn)   -- 路线上目标扫描函数
        self.spike_line_target_search_fn = fn
    end
    function hutao_com_polearm_mastery:DoSpikeLineTargetSearch(start_pt,end_pt) -- 路线上目标扫描函数
        if self.spike_line_target_search_fn then
            return self.spike_line_target_search_fn(self.inst,start_pt,end_pt)
        end
        return {}
    end
    function hutao_com_polearm_mastery:SetSpikeCasting(flag)
        self.spike_casting = flag
    end
    function hutao_com_polearm_mastery:IsSpikeCasting()
        return self.spike_casting
    end
------------------------------------------------------------------------------------------------------------------------------
--- is_running 备份用的，避免net 信道上的问题。
    function hutao_com_polearm_mastery:SetRunning(value)
        self.is_running = value
        self:DataSync()
    end
------------------------------------------------------------------------------------------------------------------------------
--- 充能值
    function hutao_com_polearm_mastery:SetCurrent(value)
        local old = self.current
        self.current = math.clamp(value,0,self.max)
    end
    function hutao_com_polearm_mastery:SetMax(value)
        self.max = math.clamp(value,0,9999)
        self.current = math.clamp(self.current,0,self.max)
    end
    function hutao_com_polearm_mastery:DoDelta(delta)
        self:SetCurrent(self.current + delta)
    end
    function hutao_com_polearm_mastery:GetCurrent()
        return self.current
    end
    function hutao_com_polearm_mastery:GetMax()
        return self.max
    end
    function hutao_com_polearm_mastery:GetPercent()
        return self.current / self.max
    end
    function hutao_com_polearm_mastery:IsFull()
        return self.current >= self.max
    end
    function hutao_com_polearm_mastery:IsEmpty()
        return self.current <= 0
    end
    function hutao_com_polearm_mastery:Clear()
        self:SetCurrent(0)
    end
------------------------------------------------------------------------------------------------------------------------------
--- 突刺伤害倍增器
    function hutao_com_polearm_mastery:SetSpikeModifier(inst,value)
        self.spike_damage_multipliers:SetModifier(inst,value)
    end
    function hutao_com_polearm_mastery:RemoveSpikeModifier(inst)
        self.spike_damage_multipliers:RemoveModifier(inst)
    end
    function hutao_com_polearm_mastery:GetSpikeModifier()
        return self.spike_damage_multipliers:Get() * 2
    end
------------------------------------------------------------------------------------------------------------------------------
----- onload/onsave 函数
    function hutao_com_polearm_mastery:AddOnLoadFn(fn)
        if type(fn) == "function" then
            table.insert(self._onload_fns, fn)
        end
    end
    function hutao_com_polearm_mastery:ActiveOnLoadFns()
        for k, temp_fn in pairs(self._onload_fns) do
            temp_fn(self)
        end
    end
    function hutao_com_polearm_mastery:AddOnSaveFn(fn)
        if type(fn) == "function" then
            table.insert(self._onsave_fns, fn)
        end
    end
    function hutao_com_polearm_mastery:ActiveOnSaveFns()
        for k, temp_fn in pairs(self._onsave_fns) do
            temp_fn(self)
        end
    end
------------------------------------------------------------------------------------------------------------------------------
----- 数据读取/储存

    function hutao_com_polearm_mastery:Get(index,default)
        if index then
            return self.DataTable[index] or default
        end
        return nil or default
    end
    function hutao_com_polearm_mastery:Set(index,theData)
        if index then
            self.DataTable[index] = theData
        end
    end

    function hutao_com_polearm_mastery:Add(index,num,min,max)
        if index then
            if min and max then
                local ret = (self.DataTable[index] or 0) + ( num or 0 )
                ret = math.clamp(ret,min,max)
                self.DataTable[index] = ret
                return ret
            else
                self.DataTable[index] = (self.DataTable[index] or 0) + ( num or 0 )
                return self.DataTable[index]
            end
        end
        return 0
    end
------------------------------------------------------------------------------------------------------------------------------

------------------------------------------------------------------------------------------------------------------------------
    function hutao_com_polearm_mastery:OnSave()
        self:ActiveOnSaveFns()
        local data =
        {
            DataTable = self.DataTable
        }
        return next(data) ~= nil and data or nil
    end

    function hutao_com_polearm_mastery:OnLoad(data)
        if data.DataTable then
            self.DataTable = data.DataTable
        end
        self:ActiveOnLoadFns()
    end
------------------------------------------------------------------------------------------------------------------------------
return hutao_com_polearm_mastery







